HistoryRoutePanel = {};
HistoryRoutePanel._isOver = true;//播放结束标志
HistoryRoutePanel._isPlay = false;//正在播放的状态
HistoryRoutePanel.timerId = null;//定时器ID
HistoryRoutePanel.playSpeed = 1000;//播放速度,
HistoryRoutePanel._autoChangePage = true;//自动翻页播放
HistoryRoutePanel.totalNum = 0;//总记录数
HistoryRoutePanel.maxTimeSpan = 1000;//最大播放间隔,每隔200ms，播放一次


HistoryRoutePanel.getRouteMapHandler = function () {
	return this.routeMapHandler;
}

HistoryRoutePanel.setPlateNo = function (simNo) {
	//$("#routePlateNo").val(simNo);
	$("#routePlateNo").textbox('setText', simNo);
}

HistoryRoutePanel.isButtonDisabled = function (btnId) {
	var opts = $("#" + btnId).linkbutton("options");
	return true == opts.disabled;
}

HistoryRoutePanel.setButtonText = function (btnId, txt) {
	$("#" + btnId + " .l-btn-text").html(txt);
}


HistoryRoutePanel.resetButton = function () {
	HistoryRoutePanel.setButtonText("btnQueryHisData", "查询");
	$("#btnQueryHisData").linkbutton('enable');
	$("#btnPlayRoute").linkbutton('disable');
	$("#btnPauseRoute").linkbutton('disable');
	$("#btnStopPlayRoute").linkbutton('disable');
}

HistoryRoutePanel.init = function () {
	var me = this;

	this.createhistoryRouteGrid();
	this._playButton = $("#btnPlayRoute");
	//  this._playButton.html("播放");
	$("#btnQueryHisData").click(function () {
		var simNo = $("#routePlateNo").textbox("getText");
		if (simNo == null || simNo.length == 0) {
			$.iMessager.alert("提示", "请输入有效车号!", 'messager-warning');
			return;
		}
		if (me.isButtonDisabled("btnQueryHisData"))
			return;

		HistoryRoutePanel.queryHistoryRoute();
	});
	$("#btnPlayRoute").click(function () {
		if (me.isButtonDisabled("btnPlayRoute"))
			return;
		//this.routeMapHandler.drawLocationWithGrid(this);
		me.play();
	});
	$("#btnStopPlayRoute").click(function () {
		if (me.isButtonDisabled("btnStopPlayRoute"))
			return;
		me.stopPlay();
	});

	$("#btnPauseRoute").click(function () {
		if (me.isButtonDisabled("btnPauseRoute"))
			return;
		me.pause();
		window.setTimeout(function () {
			me.pause();
		}, 500);
	});

	//excel导出
	$("#btnExportHisData").click(function () {
		me.exportExcel();
	});
	//播放速度滑块
	$('#playSpeedSlider').slider({
		value: 1, showTip: true,
		tipFormatter: function (value) {
			//var speed = me.maxTimeSpan - parseInt(value);
			var descr = "慢";
			if (value > 60)
				descr = "快";
			else if (value > 40)
				descr = "中";
			return descr;
		},
		onChange: function (value) {
			me.playSpeed = me.maxTimeSpan * (100 - parseInt(value)) / 100;

		}
	})


	$("#cbDisplayHisData").click(function () {

		var lastMenuClickTime = $.cookie("hisDataClickTime");
		var nowTime = new Date().getTime();
		if ((nowTime - lastMenuClickTime) >= 500) {
			$.cookie("hisDataClickTime", new Date().getTime());
			//判断是否已经打勾
			if ($("#cbDisplayHisData").prop('checked')) {
				$('#hisRouteLayout').layout('expand', 'south');
				//var tab = $("#hisDataTab").tabs("getTab",0);
				//var width = tab.width();
				//var height = tab.height();
				// me.hisDataGrid.datagrid('resize',{width:width-1,height:height-1,left:0,top:0});
			} else {
				$('#hisRouteLayout').layout('collapse', 'south');
			}
		} else {
			$.iMessager.alert('温馨提示', '操作过快，请稍后重试！', 'messager-info');
			$('#hisRouteLayout').layout('expand', 'south');
			$("#cbDisplayHisData").prop('checked', true);
		}

	});


	$('#hisRouteLayout').layout({
		onExpand: function () {
			$("#cbDisplayHisData").prop('checked', true);
		}
	})


	$('#hisRouteLayout').layout('collapse', 'south');
	//this.createhistoryRouteGrid(gridId);
	//设置在线地图路径
	$("#routeMapFrame").attr("src", path + "/hisGps/google");


	//日期类型拉框触发事件，快捷的选择日期
	$('#selDateOption').combobox(
		{
			onChange: function (newValue, oldValue) {
				var filterType = newValue;
				var now = Utility.today();
				if (filterType == "1") {
					//当天
					var startTime = now + " 00:00:00";
					var endTime = now + " 23:59:59";
					$("#routeStartTime").datetimebox('setValue', startTime);
					$("#routeEndTime").datetimebox('setValue', endTime);

				} else if (filterType == "2") {
					//昨天
					var now = new Date();
					var date1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
					date1 = Utility.addDay(date1, -1);
					var yesterday = Utility.dateToString(date1, "yyyy-MM-dd");
					var startTime = yesterday + " 00:00:00";
					var endTime = yesterday + " 23:59:59";
					$("#routeStartTime").datetimebox('setValue', startTime);
					$("#routeEndTime").datetimebox('setValue', endTime);
				} else if (filterType == "3") {
					//前天
					var now = new Date();
					var date1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
					date1 = Utility.addDay(date1, -2);
					var yesterday = Utility.dateToString(date1, "yyyy-MM-dd");
					var startTime = yesterday + " 00:00:00";
					var endTime = yesterday + " 23:59:59";
					$("#routeStartTime").datetimebox('setValue', startTime);
					$("#routeEndTime").datetimebox('setValue', endTime);

				} else if (filterType == "4") {
					//最近一天
					var date1 = new Date();
					var endTime = Utility.dateToString(date1, "yyyy-MM-dd HH:mm:ss");

					date1 = Utility.addDay(date1, -1);
					var startTime = Utility.dateToString(date1, "yyyy-MM-dd HH:mm:ss");

					$("#routeStartTime").datetimebox('setValue', startTime);
					$("#routeEndTime").datetimebox('setValue', endTime);
				} else if (filterType == "5") {
					//最近一天
					var date1 = new Date();
					var endTime = Utility.dateToString(date1, "yyyy-MM-dd HH:mm:ss");

					date1 = Utility.addDay(date1, -2);
					var startTime = Utility.dateToString(date1, "yyyy-MM-dd HH:mm:ss");

					$("#routeStartTime").datetimebox('setValue', startTime);
					$("#routeEndTime").datetimebox('setValue', endTime);
				}
			}
		});

	//当选择开始时间的时候，自动检测日期段天数只差，如果超过2天，则自动修改结束时间，
	//使其最大天数差保持为2.
	$("#routeStartTime").datetimebox({
		'onSelect': function (date) {
			var strEndTime = $("#routeEndTime").datetimebox('getText');
			startTime = date;
			endTime = Utility.stringToDate(strEndTime);
			if (isNaN(startTime.getTime()) || isNaN(endTime.getTime())) {
				$.iMessager.alert('提示', '非法的日期格式', 'messager-warning');
			}
			var iDays = Utility.getDay(startTime, endTime);
			if (iDays > 2 || iDays <= 0) {
				startTime = Utility.addDay(startTime, 1);//加一天
				var strEndTime = Utility.dateToString(startTime, "yyyy-MM-dd") + " 23:59:59";
				$("#routeEndTime").datetimebox('setValue', strEndTime);
			}

		}
	});
	//设置为当天
	var now = Utility.today();
	var startTime = now + " 00:00:00";
	var endTime = now + " 23:59:59";
	$("#routeStartTime").datetimebox('setValue', startTime);
	$("#routeEndTime").datetimebox('setValue', endTime);
}
/**
 * 根据查询条件进行excel导出
 */
HistoryRoutePanel.exportExcel = function () {
	var routeForm = $('#routeForm');
	var queryUrl = globalConfig.webPath + '/Query.mvc/paginate';
	this.routeMapHandler = this.getHisMapHandler();
	//this.routeMapHandler.clearAllElement();
	routeForm.form({
		url: queryUrl,
		onSubmit: function () {
			//进行表单验证
			//如果返回false阻止提交
			var isValid = routeForm.form('validate');
			if (isValid) {
				var simNo = $("#routePlateNo").textbox("getText");
				var startTime = $("#routeStartTime").datetimebox('getValue');
				var endTime = $("#routeEndTime").datetimebox('getValue');
				var minSpeed = $("#minSpeed").val();
				var excelFileName = "历史定位轨迹";
				var queryParams = {
					queryId: "selectHisotryGpsInfos",
					fileName: excelFileName,
					simNo: simNo, startTime: startTime, endTime: endTime, minSpeed: minSpeed, valid: 1
				};
				var strParam = jQuery.param(queryParams);
				var url = globalConfig.webPath + "/Query.mvc/export?" + strParam;
				//openWindow(url, 300,300,"");
				window.open(url);
			}
			return false;
		},
		success: function (data) {
			//alert(data)
			//me.hisDataGrid.datagrid("loadData",data);
		}
	});
	//提交表单
	$('#routeForm').submit();
}

/**
 * 历史轨迹数据分页表格
 */
HistoryRoutePanel.createhistoryRouteGrid = function () {
	//if(this.hisDataGrid)
	//return;
	var queryParams = {};//{simNo:"测A00003",startTime:"2014-09-29 1:1:1",endTime:"2014-09-29 23:1:1"};
	var queryUrl = path + '/hisGps/dataList';
	var gridId = "hisDataGrid";
	this.hisDataGrid = $("#" + gridId);
	this.hisDataGrid.datagrid({
		columns: [[
			{title: '显示名称', field: 'equipName', width: 80, minWidth: 80},
			{title: '设备号', field: 'simNo', width: 80, minWidth: 80},
			{title: '所属分组', field: 'depName', width: 120, minWidth: 80},
			{title: '时间', field: 'sendTime', width: 130, minWidth: 120},
			{title: '速度', field: 'velocity', width: 40, minWidth: 40, Visible: false},
			{title: '方向', field: 'directionDescr', width: 80, minWidth: 80},
			//{ title: '经度', field: 'locLongitude', width: 90, isSort: false, minWidth: 80 },
			//{ title: '纬度', field: 'locLatitude', width: 90, isSort: false, minWidth: 80 },
			{title: '经度', field: 'orgLongitude', width: 120},
			{title: '纬度', field: 'orgLatitude', width: 120},
		]],
		//height: 150,
		fit: true,
		url: queryUrl,
		method: 'POST',
		queryParams: queryParams,
		//idField: 'gpsId',
		striped: true,
		fitColumns: true,
		singleSelect: true,
		rownumbers: true,
		pagination: true,
		nowrap: true,
		pageSize: 50,
		pageList: [50, 100, 150, 200],
		showFooter: true,
		onBeforeLoad: function (param) {
			//在没有输入查询参数前，不查询
			if (param.simNo)
				return true;
			else
				return false;
		}, onLoadSuccess: function (data) {

			if (data.rows && data.rows.length > 0) {
				HistoryRoutePanel.startPlayRoute(data); //开始画轨迹
			} else {
				HistoryRoutePanel.resetButton();
				$.iMessager.alert('提示', '该时间段没有历史定位轨迹数据', 'messager-warning');
				return;
			}
		}
	});

	return this.hisDataGrid;
}

//设置历史轨迹播放的进度
HistoryRoutePanel.setProgress = function (value) {
	$('#routeProgressbar').progressbar('setValue', value);
}

//查询历史轨迹
HistoryRoutePanel.queryHistoryRoute = function () {
	// this.createhistoryRouteGrid();
	var me = this;
	var routeForm = $('#routeForm');
	var queryUrl = path + '/vehicle/gpsInfoList.action';
	this.routeMapHandler = this.getHisMapHandler();
	this.routeMapHandler.clearAllElement();
	routeForm.form({
		url: queryUrl,
		onSubmit: function () {
			//进行表单验证
			//如果返回false阻止提交
			var isValid = routeForm.form('validate');
			if (isValid && me.validateDate()) {
				$("#btnQueryHisData").linkbutton('disable');
				$("#btnPlayRoute").linkbutton('disable');
				me.setButtonText("btnQueryHisData", "请等待");

				var simNo = $("#routePlateNo").textbox('getText');
				var startTime = $("#routeStartTime").datetimebox('getText');
				var endTime = $("#routeEndTime").datetimebox('getText');
				var minSpeed = $("#minSpeed").val();
				var queryParams = {
					hisRoute: true,
					simNo: simNo,
					startTime: startTime,
					endTime: endTime,
					minSpeed: minSpeed
				};
				//me.hisDataGrid.load(queryParams);
				me.hisDataGrid.datagrid("load", queryParams);
			}
			return false;
		},
		success: function (data) {
			//alert(data)
			//me.hisDataGrid.datagrid("loadData",data);
		}
	});
	//提交表单
	$('#routeForm').submit();
}

HistoryRoutePanel.validateDate = function () {
	var startTime = $("#routeStartTime").datetimebox('getText');
	var endTime = $("#routeEndTime").datetimebox('getText');
	startTime = Utility.stringToDate(startTime);
	endTime = Utility.stringToDate(endTime);
	if (isNaN(startTime.getTime()) || isNaN(endTime.getTime())) {
		$.iMessager.alert('提示', '非法的日期格式', 'messager-warning');
	}
	var today = new Date();

	var date1 = new Date(startTime.getFullYear(), startTime.getMonth(), startTime.getDate());
	var date2 = new Date(endTime.getFullYear(), endTime.getMonth(), endTime.getDate());
	var date3 = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());
	if (startTime > today) {
		$.iMessager.alert('提示', '开始日期不能大于当前日期', 'messager-warning');
		return false;
	}

	if (date2 > date3) {
		$.iMessager.alert('提示', '结束日期不能大于今天日期', 'messager-warning');
		return false;
	}

	if (startTime >= endTime) {
		$.iMessager.alert('提示', '开始时间不能大于等于结束时间', 'messager-warning');
		return false;
	}

	var iDays = Utility.getDay(date1, date2);
	if (iDays > 2) {
		$.iMessager.alert('提示', '一次只能查询不超过两天的历史数据', 'messager-warning');
		return false;
	}
	return true;
}

//重新回到第一页播放
HistoryRoutePanel.firstPage = function () {
	this.hisDataGrid.datagrid("load");
}

HistoryRoutePanel.nextPage = function () {
	//this.hisDataGrid.datagrid('getPager').pagination({pageNumer:2});
	//$(".pagination-next")
	//this.hisDataGrid.datagrid('reload');
	var p = this.hisDataGrid.datagrid('getPager');
	var p1 = p.find(".pagination-next");
	p1.click();
	//p.find(".pagination-next]").click();
	//p.selectPage(
}

HistoryRoutePanel.getCurPageData = function () {
	return this.hisDataGrid.datagrid('getRows');
}

HistoryRoutePanel.getCurRowData = function () {
	return this.hisDataGrid.datagrid('getSelected');
}

HistoryRoutePanel.isLastPage = function () {
	var options = this.hisDataGrid.datagrid('getPager').data("pagination").options;
	var pageSize = options.pageSize;
	var total = options.total;
	if (pageSize == 0 || total == 0)
		return true;
	var pageNum = Math.ceil(total / pageSize);
	return options.pageNumber == pageNum;
}

HistoryRoutePanel.selectRow = function (rowNo) {
	this.hisDataGrid.datagrid('selectRow', rowNo);
}

HistoryRoutePanel.stopPlay = function () {
	HistoryRoutePanel.resetButton();

	//$("#btnPlayRoute").linkbutton('enable');
	window.clearInterval(HistoryRoutePanel.timerId);
	this.timerId = null;
	this._isOver = true;
	this._intervalId = null;
	this.firstPlay = false;
	this._isPlay = false;
	this._nCount = 0;
	this._preLat = null;
	this._preLng = null;
	this._isOver = true;
	//this._playButton.html("播放");

	var mapHandler = this.routeMapHandler;
	if (mapHandler) {
		mapHandler.drawStop();
	}
}

HistoryRoutePanel.play = function () {
	if (!this._isPlay) {
		this._isPlay = true;
		this._isPause = false;

		$("#btnPauseRoute").linkbutton('enable');
		$("#btnPlayRoute").linkbutton('disable');

		if (this._isOver == true) {
			//重新播放时，清除掉原有的轨迹
			this._isOver = false;
			this.firstPage();
			this.routeMapHandler.clearAllElement(); //
			return;
		} else {
			//从暂停处，开始播放
			var me = this;
			this.timerId = window.setInterval(function () {
				me.PlayWithGrid()
			}, this.playSpeed);
		}

	}

}

HistoryRoutePanel.pause = function () {
	$("#btnPauseRoute").linkbutton('disable');
	$("#btnPlayRoute").linkbutton('enable');
	this._isPlay = false;
	this._isPause = true;
	if (this.timerId != null) {
		window.clearInterval(this.timerId);
		this.timerId = null;
	}

}
//得到历史轨迹回放的地图对象，调用baidu.jsp中的地图对象
HistoryRoutePanel.getHisMapHandler = function () {
	var iframeDom = $("#routeMapFrame")[0];
	return iframeDom.contentWindow.OperatorObj;
}

HistoryRoutePanel.startPlayRoute = function (records) {
	$("#btnQueryHisData").linkbutton('disable');
	$("#btnPlayRoute").linkbutton('disable');
	$("#btnStopPlayRoute").linkbutton('enable');
	$("#btnPauseRoute").linkbutton('enable');
	HistoryRoutePanel.setButtonText("btnQueryHisData", "正在回放..");

	if (this.routeMapHandler == null)
		return;
	//this.routeMapHandler.setGrid(this);
	this.routeMapHandler._playButton = $("#btnPlayRoute");


	// var playSpeed = $('#playSpeed').slider("getValue");//播放速度
	//var maxTimeOut = 200;
	//this._nTimeOut = (maxTimeOut - parseInt(playSpeed));
	var p = this.hisDataGrid.datagrid('getPager');
	var options = this.hisDataGrid.datagrid('getPager').data("pagination").options;
	var curr = options.pageNumber;
	//var total = options.total;
	this.totalNum = options.total;

	var curPageData = this.getCurPageData();
	var curPageSize = curPageData.length;
	var routeMapHandler = this.routeMapHandler;
	this._nCurrentPageSize = curPageSize;
	this._nCount = 0;
	this._isOver = false;
	this._isPause = false;
	this.autoChangePage = (true);
	var me = this;
	if (this.timerId) {
		routeMapHandler.drawStop();
		this._timerId = null;
	}
	//console.log("创建计时器:"+ routeMapHandler.timerId);
	this.timerId = setInterval(
		function () {
			me.PlayWithGrid();
		},
		this.playSpeed);


}


HistoryRoutePanel.convertRecord = function (rd) {
	// var vehicleInfo = {id:rd.ID, text:rd.simNo, vehicleId:rd.ID, rLat:rd.latitude,rLng:rd.longitude, tLat:rd.latitude, tLng:rd.longitude,status:rd.status,color:rd.plateColor,validate:rd.valid,direction:rd.direction,
	//  angleInt:rd.direction, statusInt:0, speed:rd.velocity, warnTypeId:0, online:rd.online};
	// return vehicleInfo;

}

HistoryRoutePanel.PlayWithGrid = function () {
	if (this._isOver)
		return;

	if (this._nCount == this._nCurrentPageSize) {
		//当前页的记录已经播放完毕，需要翻页，继续播放
		if (this.isLastPage() == false) {
			if (this._autoChangePage) {
				this._nCount = 0;
				this._isPlay = false;
				if (this.timerId != null) {
					window.clearInterval(this.timerId);
					this.timerId = null;
				}
				this.nextPage();
			} else {
				//this._drawPlayWithGrigEnd();
			}
		} else {
			this.stopPlay();//播放结束
		}
	} else {

		if (!this._isPlay)
			this._isPlay = true;
		//设置表格显示正在播放的行上
		//this._gridSelectionModel.select(this._nCount, true);
		this.selectRow(this._nCount);
		//var index = this._nCount + (this._nCurrentPage - 1) * this._defaultPageSize;
		var record = this.getCurRowData();
		if (record) {
			record.online = true;
			this.routeMapHandler.drawRoute(record);//画出历史轨迹
			if (this.totalNum > 0) {
				var progress = parseInt(100 * (record.rowNum / this.totalNum));
				this.setProgress(progress);
			}
		}
	}
	this._nCount++;
}
